﻿// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Website: https://www.blazor.zone or https://argozhang.github.io/

using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Routing;

namespace BootstrapBlazor.Components;

/// <summary>
///  <see cref="NavbarLink"/> 组件
///  可点击的链接，<see cref="NavbarItem"/> 或 <see cref="NavbarDropdown"/> 的兄弟组件
/// </summary>
public sealed partial class NavbarLink
{
    /// <summary>
    /// 获得 样式类集合 
    /// </summary>
    /// <returns></returns>
    private string? ClassString => CssBuilder.Default()
        .AddClass("nav-link", ParentItemState.IsHorizontal && !InDropdown)
        .AddClass("navbar-v-link", !ParentItemState.IsHorizontal && !InDropdown)
        .AddClass("disabled", ParentItemState.Disabled && !InDropdown)
        .AddClassFromAttributes(AdditionalAttributes)
        .Build();

    /// <summary>
    /// 获得 样式集合
    /// </summary>
    /// <returns></returns>
    private string? StyleClassString => CssBuilder.Default()
        .AddStyleFromAttributes(AdditionalAttributes)
        .Build();

    /// <summary>
    /// 获得/设置 父级 <see cref="NavbarItem"/> 组件的状态
    /// </summary>
    [CascadingParameter]
    public NavbarItemState ParentItemState { get; set; } = new();

    /// <summary>
    /// 获得/设置 是否在下拉菜单中使用
    /// </summary>
    [Parameter]
    public string? Key { get; set; }

    /// <summary>
    /// 获得/设置 是否在下拉菜单中使用
    /// </summary>
    [Parameter]
    public bool InDropdown { get; set; }

    /// <summary>
    /// 获得/设置 是否显示Tooltip
    /// </summary>
    [Parameter]
    public bool Tooltip { get; set; }

    /// <summary>
    /// 获得/设置 点击事件
    /// </summary>
    [Parameter]
    public EventCallback Clicked { get; set; }

    /// <summary>
    /// 获得/设置 指定链接指向的页面URL
    /// </summary>
    [Parameter]
    public string? To { get; set; }

    /// <summary>
    /// 获得/设置 链接指定打开的目标
    /// </summary>
    [Parameter]
    public NavLinkTarget Target { get; set; } = NavLinkTarget.None;

    /// <summary>
    /// 获得/设置 URL匹配方式。
    /// </summary>
    [Parameter]
    public NavLinkMatch Match { get; set; } = NavLinkMatch.All;

    /// <summary>
    /// 获得/设置 额外提示信息
    /// </summary>
    [Parameter]
    public string? Title { get; set; }

    /// <summary>
    /// 获得/设置 子组件内容
    /// </summary>
    [Parameter]
    public RenderFragment? ChildContent { get; set; }

    /// <summary>
    /// 延迟到组件渲染完成后执行的方法队列
    /// </summary>
    private Queue<Func<ValueTask>>? ExecuteAfterRenderQueue { get; set; }

    /// <summary>
    /// 锚点目标
    /// </summary>
    private string? AnchorTarget { get; set; }

    /// <summary>
    /// OnParametersSet
    /// </summary>
    protected override void OnParametersSet()
    {
        if (AdditionalAttributes != null && AdditionalAttributes.TryGetValue("href", out var href))
        {
            To = $"{href}";
        }

        if (!string.IsNullOrWhiteSpace(To) && To.StartsWith("#"))
        {
            AnchorTarget = To[1..];
        }

        base.OnParametersSet();

        if (!InDropdown && !string.IsNullOrWhiteSpace(Title))
        {
            ExecuteAfterRender(ToggleTooltip);
        }
    }

    /// <summary>
    /// OnAfterRenderAsync
    /// </summary>
    /// <param name="firstRender"></param>
    /// <returns></returns>
    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        await base.OnAfterRenderAsync(firstRender);

        //执行延迟的方法
        if (ExecuteAfterRenderQueue?.Count > 0)
        {
            while (ExecuteAfterRenderQueue.Count > 0)
            {
                await ExecuteAfterRenderQueue.Dequeue()();
            }
        }
    }

    /// <summary>
    /// 延迟到组件渲染完成后执行
    /// </summary>
    /// <param name="action"></param>
    private void ExecuteAfterRender(Func<ValueTask> action)
    {
        if (ExecuteAfterRenderQueue == null)
        {
            ExecuteAfterRenderQueue = new();
            ExecuteAfterRenderQueue.Enqueue(action);
        }
        else if (!ExecuteAfterRenderQueue.Contains(action))
        {
            ExecuteAfterRenderQueue.Enqueue(action);
        }
    }

    /// <summary>
    /// 调用 $.bb_tooltip 脚本方法
    /// </summary>
    /// <returns></returns>
    private ValueTask ToggleTooltip()
    {
        var visible = (ParentItemState.IsVerticalMixin && !ParentItemState.Visible) || ParentItemState.IsVerticalSmall || Tooltip;
        var placement = (ParentItemState.IsRTL ? Placement.Left : Placement.Right).ToDescriptionString();
        return JSRuntime.InvokeVoidAsync(Id, "bb_toggle_navbar_link_tooltip", visible, placement);
    }
}
